package com.yuan.service.impl;

import com.yuan.pojo.User;
import com.yuan.dao.UserDao;
import com.yuan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Transactional(
            readOnly = false,
            propagation = Propagation.REQUIRED,
            isolation = Isolation.REPEATABLE_READ
    )
    @Override
    public void updateUser(User user) {
        //通过用户名修改密码
        userDao.updatePwdByUsername(user.getUsername(), user.getPassword());

        //出错啦
        int i = 1/0;

        //通过用户名修改昵称
        userDao.updateNicknameByUsername(user.getUsername(), user.getNickname());
    }

    //通过用户名修改密码
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public void updatePassWord(User user) {

        userDao.updatePwdByUsername(user.getUsername(), user.getPassword());
        // 修改密码后 手动调用修改昵称
//        userDao.updateUserName(user);

    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    //通过用户名修改昵称
    @Override
    public void updateUserName(User user) {
        // bug
        int i = 1/0;
        userDao.updateNicknameByUsername(user.getNickname(), user.getUsername());
    }
}
